package net.csdn.business.discuss.api.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.csdn.business.discuss.api.mapper.DiscussVoteOptionMapper;
import net.csdn.business.discuss.api.model.dto.NewDiscussVoteOptionDTO;
import net.csdn.business.discuss.api.model.dto.UpdateDiscussVoteOptionDTO;
import net.csdn.business.discuss.api.model.entity.DiscussVoteOption;
import net.csdn.business.discuss.api.model.query.DiscussVoteOptionQuery;
import net.csdn.business.discuss.api.model.vo.DiscussVoteOptionVO;
import net.csdn.business.discuss.api.service.IDiscussVoteOptionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/**
 * 投票选项Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-25
 */
@Service
public class DiscussVoteOptionServiceImpl extends ServiceImpl<DiscussVoteOptionMapper, DiscussVoteOption> implements IDiscussVoteOptionService
{

    /**
     * 分页查询投票选项列表
     *
     * @param query 投票选项
     * @return 投票选项
     */
    @Override
    public IPage<DiscussVoteOptionVO> findPageList(DiscussVoteOptionQuery query) {
        //获得Page对象
        Page<DiscussVoteOptionVO> page = query.getPage();
        List<DiscussVoteOptionVO> pageList=baseMapper.findPageList(page,query);
        page.setRecords(pageList);
        return page;
    }

    /**
     * 根据id查询投票选项
     * 
     * @param id 投票选项主键
     * @return 投票选项
     */
    @Override
    public DiscussVoteOptionVO selectDiscussVoteOptionById(String id)
    {
        return baseMapper.selectDiscussVoteOptionById(id);
    }

    /**
     * 查询投票选项列表
     * 
     * @param discussId 投票选项
     * @return 投票选项
     */
    @Override
    public LinkedList<DiscussVoteOptionVO> selectVoteOptionList(String discussId)
    {
        LinkedList<DiscussVoteOptionVO> result=baseMapper.selectVoteOptionList(discussId);
        getDoubleRatio(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addBatch(NewDiscussVoteOptionDTO params) {
        LinkedList<String> options=params.getOptions();
        if(options==null||options.size()==0){
            return 0;
        }
        List<DiscussVoteOption> voteOptions=new ArrayList<>();
        int order=0;
        for (String option :options) {
            if(StringUtils.isNotBlank(option)){
                order++;
                DiscussVoteOption voteOption=new DiscussVoteOption();
                voteOption.setVoteOption(option);
                voteOption.setOptionOrder(order);
                String id= IdUtil.fastSimpleUUID();
                voteOption.setObjectId(id);
                voteOption.setVoteId(params.getVoteId());
                voteOption.setDiscussId(params.getDiscussId());
                voteOption.setCreatedBy(params.getCurrUserId());
                voteOptions.add(voteOption);
            }
        }
        return baseMapper.addBatch(voteOptions);
    }

    /**
     * 新增投票选项
     *
     * @param newDiscussVoteOptionDTO 投票选项
     * @return 结果
     */
    @Override
    public Integer insertDiscussVoteOption(NewDiscussVoteOptionDTO newDiscussVoteOptionDTO)
    {
        DiscussVoteOption discussVoteOption=new DiscussVoteOption();
        BeanUtils.copyProperties(newDiscussVoteOptionDTO,discussVoteOption);
        return baseMapper.insertDiscussVoteOption(discussVoteOption);
    }

    /**
     * 修改投票选项
     *
     * @param updateDiscussVoteOptionDTO 投票选项
     * @return 结果
     */
    @Override
    public Integer updateDiscussVoteOption(UpdateDiscussVoteOptionDTO updateDiscussVoteOptionDTO)
    {
        DiscussVoteOption discussVoteOption=new DiscussVoteOption();
        BeanUtils.copyProperties(updateDiscussVoteOptionDTO,discussVoteOption);
        return baseMapper.updateDiscussVoteOption(discussVoteOption);
    }



    public void getDoubleRatio(LinkedList<DiscussVoteOptionVO> options) {
        if(options==null||options.size()==0){
            return;
        }
        NumberFormat instance = NumberFormat.getInstance();
        // 保留小数点后两位（四舍五入），
        instance.setMaximumFractionDigits(2);
        double sum = options.stream().map(option -> option.getVoteTotal()).mapToDouble(Integer::doubleValue).sum();
        if(sum==0){
            return;
        }
        // 计算各自所占百分比
        double ratioSum = 0;
        int length=options.size();
        for (int i = 0; i < length; i++) {
           /* if (length > 1 && i == length - 1) {
                DiscussVoteOptionVO discussVoteOption=options.get(i);
                String ratioStr = instance.format(1 - ratioSum);
                double retio = Double.parseDouble(ratioStr);
                discussVoteOption.setVoteTotalPercent(retio);
            } else {*/
                DiscussVoteOptionVO discussVoteOption=options.get(i);
                Integer voteTotal=discussVoteOption.getVoteTotal();
                String ratioStr = instance.format(voteTotal / sum);
                double retio = Double.parseDouble(ratioStr);
                ratioSum = ratioSum + retio;
                discussVoteOption.setVoteTotalPercent(retio);
           /* }*/
        }
    }

}
